package queryfish.user.view;

import java.awt.Cursor;
import java.awt.ScrollPane;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.ResourceBundle.Control;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

import queryfish.antlr.parser.analysisQues;
import queryfish.db.db4o.Answer;
import queryfish.db.db4o.Connection;
import queryfish.db.db4o.Query;
import queryfish.google.search.GoogleSearch;

import java.awt.Color;
import java.io.IOException;
import java.io.File;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;


//* To change this template, choose Tools | Templates
//* and open the template in the editor.
// */

/**
 * 
 * @author DuyThanh
 */
public class Home extends javax.swing.JFrame {
	public long start, end;
	public static String selectedName = "";

	/**
	 * Creates new form Home
	 */
	public Home() {
		getContentPane().setBackground(new Color(51, 102, 0));
		setResizable(false);
		try {
    		JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File(".\\image\\backgroundpts1.png"))));
    		this.setContentPane(label);
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
		initComponents();
		ImageIcon icon = new ImageIcon (".\\image\\iconFish.png");
		this.setIconImage(icon.getImage());
		
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		btnQuery = new javax.swing.JButton();
		jMenuBar1 = new javax.swing.JMenuBar();
		mnGuide = new javax.swing.JMenu();
		mnInfor = new javax.swing.JMenu();
		mnExit = new javax.swing.JMenu();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("TRUY VẤN KIẾN THỨC CÁ CẢNH");

		btnQuery.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
		btnQuery.setForeground(new java.awt.Color(0, 0, 204));
		btnQuery.setText("Truy Vấn");
		btnQuery.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnQueryActionPerformed(evt);
			}
		});

		mnGuide.setText("Hướng Dẫn");
		mnGuide.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				mnGuideMouseClicked(evt);
			}
		});
		jMenuBar1.add(mnGuide);

		mnInfor.setText("Thông Tin");
		mnInfor.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				mnInforMouseClicked(evt);
			}
		});
		jMenuBar1.add(mnInfor);

		mnExit.setText("Thoát");
		mnExit.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				mnExitMouseClicked(evt);
			}
		});
		jMenuBar1.add(mnExit);

		setJMenuBar(jMenuBar1);
		txtAnswer = new javax.swing.JTextArea();
		
				txtAnswer.setColumns(20);
				txtAnswer.setRows(5);
				txtAnswer.setEditable(false);
				txtAnswer.setLineWrap(true);
				txtAnswer.setWrapStyleWord(true);
				txtAnswer.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				txtAnswer.setBackground(new Color(255, 255, 255, 90));
		
				
				
		lstImage = new javax.swing.JList();
		//
		lstImage.setOpaque(false);
		lstImage.setBorder(new LineBorder(new Color(0, 0, 0)));
		lstImage.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				lstImageMouseClicked(evt);
			}
		});
		txtQuestion = new javax.swing.JTextField();
		//txtQuestion.setBorder(new EmptyBorder(0, 0, 0, 0));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(layout.createSequentialGroup()
								.addGap(34)
								.addComponent(txtAnswer, 0, 0, Short.MAX_VALUE))
							.addGroup(layout.createSequentialGroup()
								.addGap(24)
								.addComponent(txtQuestion, GroupLayout.PREFERRED_SIZE, 602, GroupLayout.PREFERRED_SIZE)))
						.addGroup(layout.createSequentialGroup()
							.addGap(290)
							.addComponent(btnQuery, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)))
					.addGap(32)
					.addComponent(lstImage, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE)
					.addGap(567))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(56)
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(lstImage, GroupLayout.PREFERRED_SIZE, 391, GroupLayout.PREFERRED_SIZE)
						.addGroup(layout.createSequentialGroup()
							.addComponent(txtQuestion, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
							.addGap(60)
							.addComponent(btnQuery, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
							.addGap(16)
							.addComponent(txtAnswer, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(529, Short.MAX_VALUE))
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>

	public void showImage(ArrayList<String> arrImage, ArrayList<String> arrName) {
		// load image vào Jlist
		DefaultListModel lstModel = new DefaultListModel();// new list model
		for (int i = 0; i < arrImage.size(); i++) {
			String path = "Resource//Image//" + arrImage.get(i).toString()
					+ ".jpg";
			String fishName = arrName.get(i).toString();
			Image ii = new Image(fishName, path);
			lstModel.addElement(ii);// add đối tượng image vào list model
		}
		lstImage.setModel(lstModel);
		lstImage.setCellRenderer(new ImageRenderer());
		lstImage.setVisibleRowCount(arrImage.size());
		
	}

	public void freeMemory() {
		Query.arrFishname.clear();
		Query.arrResult.clear();
		Query.imageName.clear();
		Answer.answer = "";
		GoogleSearch.arrTitle.clear();
		GoogleSearch.arrURL.clear();
		analysisQues.keyworld="";
		analysisQues.nameFish="";
		Connection.many="";

	}

	private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		// xử lý button Truy Vấn
		freeMemory();
		Runtime.getRuntime().gc();
		start = System.currentTimeMillis();
		if (txtQuestion.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "Chưa nhập câu hỏi !",
					"Warning", JOptionPane.OK_OPTION);
		} else {
			String question = txtQuestion.getText();
			Connection.loadData(question);
			if (Connection.test == true)
				txtAnswer.setText(Answer.answer);
			else txtAnswer.setText("kiểm tra lại câu hỏi đã nhập");

		}
		showImage(Query.imageName, Query.arrFishname);
		end = System.currentTimeMillis();
		System.out.println("Time Millis: " + (end - start));
		freeMemory();
		Runtime.getRuntime().gc();
	}

	private void mnExitMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		// xử lý menu Thoát

	}

	private void mnInforMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		// Xử lý menu Thông Tin

	}

	private void mnGuideMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		// Xử lý menu Hướng Dẫn

	}

	private void lstImageMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Home.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Home.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Home.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Home.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				Home home = new Home();
				home.setVisible(true);
				home.setLocationRelativeTo(null);
				home.setResizable(false);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnQuery;
	private javax.swing.JMenuBar jMenuBar1;
	public javax.swing.JList<ImageIcon> lstImage;
	private javax.swing.JMenu mnExit;
	private javax.swing.JMenu mnGuide;
	private javax.swing.JMenu mnInfor;
	private javax.swing.JTextArea txtAnswer;
	private javax.swing.JTextField txtQuestion;
}
//
//

